<?php
namespace application\controllers;

use system\core\Controller;
use system\libraries\FormValidation;
use application\models\AuthModel;

class Auth extends Controller
{
    public function login()
    {
        echo '<hr />Logged in with role: ' . $this->session->data('role_id');
        $fv = new FormValidation();
        $fv->rules('username', 'Username', 'required');
        $fv->rules('password', 'Password', 'required|callback_Authenticate');
        
        if ( $fv->run() )
        {
            $this->session->setData('role_id', 'user');
            $this->session->setData('logged_in', TRUE);
            redirect('auth/login');
        }
    }
    
    public function callback_Authenticate( $password )
    {
        $authModel = new AuthModel();
        
        if ( $authModel->authenticate($_POST['username'], $password) )
        {
            return TRUE;
        }
        
        return 'Username and/or password are incorrect.';
    }
    
    public function logout()
    {
        $this->session->destroy();
        redirect('auth/login');
        $this->template->disable();
    }
}

/* End of file Auth.php */
